package com.fr.data.core.db.dialect;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.data.core.db.dml.Table;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MySQLDialect extends AbstractDialect
{
  private String defaultAutoIncrease = "unsigned ";
  private String defaultAutoIncreaseEM = "auto_increment";
  private static Map yearDataMap = new HashMap();

  public MySQLDialect()
  {
    this.left_quote = "'";
    this.right_quote = "'";
  }

  public String column2SQL(String paramString)
  {
    if (StringUtils.isBlank(paramString))
      return "";
    if ((paramString.startsWith("'|\\\"")) && (paramString.endsWith("'|\\\"")))
      return "'" + paramString.trim().toUpperCase().substring(1, paramString.length() - 1) + "'";
    return paramString;
  }

  public String columnType2SQL(int paramInt, String paramString)
  {
    switch (paramInt)
    {
    case -2:
      return "TINYBLOB";
    case 2004:
      return "MEDIUMBLOB";
    case -4:
      return "LONGBLOB";
    case 2005:
      return "longtext";
    case 4:
      return "int";
    case -1:
      return "longtext";
    case 1:
      return "varchar(" + paramString + ")";
    case 12:
      return "varchar(" + paramString + ")";
    case -3:
      return "varbinary(" + paramString + ")";
    case 3:
      return "decimal(" + paramString + ")";
    case 2:
      return "numeric(" + paramString + ")";
    case 7:
      return "FLOAT";
    case -5:
      return "BIGINT(" + paramString + ")";
    case 5:
      return "SMALLINT(" + paramString + ")";
    case -7:
      return "VARCHAR(1)";
    }
    return TypeUtils.getTypeName(paramInt);
  }

  public String table2SQL(Table paramTable)
  {
    return super.table2SQL(paramTable);
  }

  public String createSequence(Connection paramConnection, String paramString1, String paramString2, String paramString3)
  {
    return paramString3 + " " + this.defaultAutoIncrease + this.defaultAutoIncreaseEM;
  }

  public String getIdentitySelectString()
  {
    return "select last_insert_id()";
  }

  public boolean supportsLimit()
  {
    return true;
  }

  public String getLimitString(String paramString, int paramInt1, int paramInt2)
  {
    return (paramString.length() + 20) + paramString + new StringBuffer().append(" limit ").append(paramInt1).append(", ").append(paramInt2).toString();
  }

  public boolean isYearData(Connection paramConnection, int paramInt, Table paramTable, String paramString)
  {
    if (paramInt != 91)
      return false;
    CTC localCTC = new CTC(this, paramConnection, paramTable, paramString);
    if (yearDataMap.get(localCTC) == null)
    {
      boolean bool = isYear(paramConnection, paramTable, paramString);
      yearDataMap.put(localCTC, new Boolean(bool));
      return bool;
    }
    return ((Boolean)yearDataMap.get(localCTC)).booleanValue();
  }

  private boolean isYear(Connection paramConnection, Table paramTable, String paramString)
  {
    String str = "select " + paramString + " from " + paramTable.toString();
    PreparedStatement localPreparedStatement = null;
    try
    {
      localPreparedStatement = paramConnection.prepareStatement(str);
      ResultSet localResultSet = localPreparedStatement.executeQuery();
      ResultSetMetaData localResultSetMetaData = localResultSet.getMetaData();
      if (localResultSetMetaData.getColumnTypeName(1).equalsIgnoreCase("YEAR"))
      {
        int i = 1;
        jsr 72;
        return i;
      }
    }
    catch (SQLException localSQLException1)
    {
      FRContext.getLogger().log(Level.WARNING, localSQLException1.getMessage(), localSQLException1);
      try
      {
        localPreparedStatement.close();
      }
      catch (SQLException localSQLException2)
      {
        FRContext.getLogger().log(Level.WARNING, localSQLException2.getMessage(), localSQLException2);
      }
    }
    finally
    {
      try
      {
        localPreparedStatement.close();
      }
      catch (SQLException localSQLException3)
      {
        FRContext.getLogger().log(Level.WARNING, localSQLException3.getMessage(), localSQLException3);
      }
    }
    return false;
  }

  public String quartzDelegateClass()
  {
    return "com.fr.third.org.quartz.impl.jdbcjobstore.MSSQLDelegate";
  }

  private class CTC
  {
    private String url;
    private String user;
    private Table table;
    private String columnName;
    private final MySQLDialect this$0;

    public CTC(, Connection paramConnection, Table paramTable, String paramString)
    {
      this.this$0 = paramMySQLDialect;
      this.url = null;
      this.user = null;
      this.table = null;
      this.columnName = null;
      try
      {
        this.url = paramConnection.getMetaData().getURL();
        this.user = paramConnection.getMetaData().getUserName();
      }
      catch (SQLException localSQLException)
      {
        FRContext.getLogger().log(Level.WARNING, localSQLException.getMessage(), localSQLException);
      }
      this.table = paramTable;
      this.columnName = paramString;
    }

    public int hashCode()
    {
      int i = 0;
      i = 37 * i + this.url.hashCode();
      i = 37 * i + this.user.hashCode();
      i = 37 * i + this.table.hashCode();
      i = 37 * i + this.columnName.hashCode();
      return i;
    }

    public boolean equals()
    {
      if (!(paramObject instanceof CTC))
        return false;
      CTC localCTC = (CTC)paramObject;
      return ((ComparatorUtils.equals(this.url, localCTC.url)) && (ComparatorUtils.equals(this.user, localCTC.user)) && (ComparatorUtils.equals(this.table, localCTC.table)) && (ComparatorUtils.equals(this.columnName, localCTC.columnName)));
    }
  }
}